package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayFindProduct;
import org.itboys.mobile.service.mongo.web.SundayFindProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/24
 * Description:
 */
@Controller
@RequestMapping("/sunday/web/findProduct")
public class SundayFindProductController  extends BaseController {
    @Autowired
    private SundayFindProductService findProductService;

    /**
     * 楼盘预约记录查询页
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        HttpServletResponse response
    ){
        return "/sunday/findProduct/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ){
        try {
            PageResult<SundayFindProduct> result = findProductService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/findProduct/select------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    /**
     * 设置是否已经回执用户
     * @param request
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping("/setStatus")
    public void setStatus(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value="id",required=true) Long id
            ,@RequestParam(value="status",required=true) Integer status
    ) {
        try {
            findProductService.setStatus(id,status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/findProduct/setStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    
}
